#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@his-pc>
#
# Distributed under terms of the MIT license.

"""

"""


class Solution:
    def longestConsecutive(self, nums):
        self.nums_set = set(nums)

        longest = 0

        while self.nums_set:
            # NOTE: 注意这里num已经pop过了
            num = self.nums_set.pop()
            ret = self.process_num(num)
            longest = max(longest, ret)

        return longest

    def process_num(self, num):
        # NOTE: num在进入之前已经被pop过了
        # [most_left, most_right) : 左闭右开
        most_left = left = num - 1
        most_right = right = num + 1

        while left in self.nums_set:
            self.nums_set.remove(left)
            left -= 1
        most_left = left + 1        # 左闭

        while right in self.nums_set:
            self.nums_set.remove(right)
            right += 1
        most_right = right          # 右开

        return most_right - most_left


def main():
    print("start main")
    l = [100, 4, 200, 1, 3, 2]
    s = Solution()
    ret = s.longestConsecutive(l)
    print(ret)


if __name__ == "__main__":
    main()
